﻿using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent]
public class UIElement : MonoBehaviour
{
    public GameObject[] ObjectList;
    public UISprite[] SpriteList;
    public UILabel[] LabelList;
    public int ElementId;
    
    public void SetObjActive(int nIndex, bool bActive)
    {
        if (nIndex >= 0 && nIndex < ObjectList.Length)
        {
            var go = ObjectList[nIndex];
            Utility.SetActive(go, bActive);
        }
    }

    public void SetSpriteActive(int nIndex, bool bActive)
    {
        if (nIndex >= 0 && nIndex < SpriteList.Length)
        {
            Utility.SetActive(SpriteList[nIndex], bActive);
        }
    }

    public void SetLabelActive(int nIndex, bool bActive)
    {
        if (nIndex >= 0 && nIndex < LabelList.Length)
        {
            Utility.SetActive(LabelList[nIndex], bActive);
        }
    }

    public void SetSprite(int nIndex, string strName)
    {
        if (nIndex >= 0 && nIndex < SpriteList.Length)
        {
            SpriteList[nIndex].spriteName = strName;
        }
    }

    public void SetLabel(int nIndex, string strText)
    {
        if (nIndex >= 0 && nIndex < LabelList.Length)
        {
            LabelList[nIndex].text = strText;
        }
    }

    public T GetComponentInSelf<T>(int nIndex) where T : Component
    {
        if (nIndex >= 0 && nIndex < ObjectList.Length)
        {
            return ObjectList[nIndex].GetComponent<T>();
        }
        return null;
    }

    public void GetComponentsInChild<T>(List<T> results, int nIndex,
        bool includeInactive = true) where T : Component
    {
        if (nIndex >= 0 && nIndex < ObjectList.Length)
        {
            ObjectList[nIndex].GetComponentsInChildren(includeInactive, results);
        }
    }
}